gh-149083: Convert dataclasses.MISSING and dataclasses.KW_ONLY to sentinels#149086
Merged
JelleZijlstra merged 1 commit intopython:mainfrom Apr 30, 2026
Merged
gh-149083: Convert dataclasses.MISSING and dataclasses.KW_ONLY to sentinels#149086JelleZijlstra merged 1 commit intopython:mainfrom
JelleZijlstra merged 1 commit intopython:mainfrom
Conversation
…to sentinels There were comments claiming these were implemented as custom classes to give a nicer repr(), but the repr() wasn't all that nice: >>> repr(dataclasses.MISSING) '<dataclasses._MISSING_TYPE object at 0x1005e7e00>' >>> repr(dataclasses.KW_ONLY) '<dataclasses._KW_ONLY_TYPE object at 0x100884050>' Sentinels are conceptually the right tool for these, so let's use them. This does change the repr() of these two objects.
Member
|
Thanks, @JelleZijlstra. My only concern: is there any chance this will cause a performance regression when importing dataclasses? I assume not, since there are no imports involved, but I thought I'd ask. People are understandably touchy about it. |
Member
Author
|
It's almost certainly faster. Creating a class is quite slow; creating a sentinel is a very simple C call. From So this might actually measurably improve import time. |
ericvsmith
approved these changes
Apr 30, 2026
|
|
||
| def test_missing_repr(self): | ||
| self.assertIn('MISSING_TYPE object', repr(MISSING)) | ||
| self.assertEqual(repr(MISSING), 'MISSING') |
Member
There was a problem hiding this comment.
I have no idea why the arguments were in the opposite order before, but changing them is a definite improvement!
Member
There was a problem hiding this comment.
And yes, I realize that wasn't really the point of this change.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
There were comments claiming these were implemented as custom classes to give a nicer
repr(), but the repr() wasn't all that nice:
Sentinels are conceptually the right tool for these, so let's use them.
This does change the repr() of these two objects.